unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, XMLConf, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, Menus, ComCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    ColorDialog1: TColorDialog;
    FindDialog1: TFindDialog;
    FontDialog1: TFontDialog;
    MainMenu1: TMainMenu;
    Memo1: TMemo;
    mnuFind: TMenuItem;
    mnuAbout: TMenuItem;
    mnuFontColor: TMenuItem;
    mnuFont: TMenuItem;
    mnuView: TMenuItem;
    mnuSelectAll: TMenuItem;
    mnuPaste: TMenuItem;
    mnuCut: TMenuItem;
    mnuCopy: TMenuItem;
    mnuEdit: TMenuItem;
    mnuExit: TMenuItem;
    mnuSave: TMenuItem;
    mnuOpen: TMenuItem;
    mnuFile: TMenuItem;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    StatusBar1: TStatusBar;
    XMLConfig1: TXMLConfig;
    procedure FindDialog1Find(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure mnuAboutClick(Sender: TObject);
    procedure mnuCopyClick(Sender: TObject);
    procedure mnuCutClick(Sender: TObject);
    procedure mnuExitClick(Sender: TObject);
    procedure mnuFindClick(Sender: TObject);
    procedure mnuFontClick(Sender: TObject);
    procedure mnuFontColorClick(Sender: TObject);
    procedure mnuOpenClick(Sender: TObject);
    procedure mnuPasteClick(Sender: TObject);
    procedure mnuSaveClick(Sender: TObject);
    procedure mnuSelectAllClick(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;

implementation
uses Unit2;
{ TForm1 }

procedure TForm1.mnuOpenClick(Sender: TObject);
begin
  if OpenDialog1.Execute then Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
end;

procedure TForm1.mnuPasteClick(Sender: TObject);
begin
  Memo1.PasteFromClipboard;
end;

procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
var rez : TModalResult;
begin
  if  Memo1.Modified then begin
    rez := MessageDlg('Сохранить файл?', mtConfirmation, [mbYes, mbNo],0);
    if rez =mrYes then
      if SaveDialog1.Execute then Memo1.Lines.SaveToFile(SaveDialog1.FileName);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  XMLConfig1.Filename:='config.xml';
  Memo1.Font.Size:=XMLConfig1.GetValue('Font/Size',0);
  Memo1.Font.Name:=XMLConfig1.GetValue('Font/Name','');
 Memo1.Font.Color:=XMLConfig1.GetValue('Font/Color',StringToColor('clDefault'));
end;

procedure TForm1.mnuAboutClick(Sender: TObject);
begin
  Form2.show;
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
var i : Integer;
begin
  for i:=0 to Memo1.Lines.Count-1 do begin
    if Pos(FindDialog1.FindText, Memo1.Lines.Text) > 0 then begin
       Memo1.SetFocus;
       Memo1.SelStart:=Pos(FindDialog1.FindText, Memo1.Lines.Text)-1;
       Memo1.SelLength:=Length(FindDialog1.FindText);
    end;
  end;

end;

procedure TForm1.mnuCopyClick(Sender: TObject);
begin
  Memo1.CopyToClipboard;
end;

procedure TForm1.mnuCutClick(Sender: TObject);
begin
  Memo1.CutToClipboard;
end;

procedure TForm1.mnuExitClick(Sender: TObject);
begin
  Application.Terminate;
end;

procedure TForm1.mnuFindClick(Sender: TObject);
begin
  FindDialog1.Execute;
end;

procedure TForm1.mnuFontClick(Sender: TObject);
begin
  if FontDialog1.Execute then Memo1.Font:=FontDialog1.Font;
  // сохранение настроек
  XMLConfig1.Filename:='config.xml';
  XMLConfig1.Clear;
  XMLConfig1.SetValue('Font/Name', Memo1.Font.Name);
  XMLConfig1.SetValue('Font/Size', Memo1.Font.Size);
  XMLConfig1.SetValue('Font/Color',ColorToString(Memo1.Font.Color));
  XMLConfig1.Flush;

end;

procedure TForm1.mnuFontColorClick(Sender: TObject);
begin
  if ColorDialog1.Execute then Memo1.Font.Color:=ColorDialog1.Color;
end;

procedure TForm1.mnuSaveClick(Sender: TObject);
begin
  if SaveDialog1.Execute then Memo1.Lines.SaveToFile(SaveDialog1.FileName);
end;

procedure TForm1.mnuSelectAllClick(Sender: TObject);
begin
  Memo1.SelectAll;
end;

initialization
  {$I unit1.lrs}

end.

